﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using Bxas.Utility.Web.Keepers;
using DigitalAmele.BLL.Core;
using System.Web.UI.WebControls;
using DigitalAmele.BLL.Adapters;

//Sami
namespace DigitalAmele.WebApp.Core
{

    public class MemberUser
    {
        public Members Member;
        public List<Roles> FolderRoles;
        public List<Roles> PageRoles;
        public List<Roles> MenuRoles;
        public List<Roles> ProcessingRoles;

        public MemberUser()
        {
            if (Member == null)
                Member = new Members();

            if (FolderRoles == null)
                FolderRoles = new List<Roles>();
            if (PageRoles == null)
                PageRoles = new List<Roles>();
            if (MenuRoles == null)
                MenuRoles = new List<Roles>();
            if (ProcessingRoles == null)
                ProcessingRoles = new List<Roles>();
        }
    }

    public partial class BasePage : Page
    {
        protected String ServerName = "http:\\localhost";
        public MemberUser memberUser = null;
        public BasePage()
        {
        }

        /// <summary>
        /// Connection string erişimi
        /// </summary>
        public string ConStr
        {
            get
            {
                return System.Configuration.ConfigurationManager.AppSettings["dbConnection"];
            }
        }

        protected void throw_out_user()
        {
            Bxas.Utility.Web.Keepers.SessionKeeper.clear_all();
            HttpContext.Current.Response.Redirect("~/Login.aspx");
        }

        #region Overlay
        protected void close_overlay(UpdatePanel up)
        {
            ScriptManager.RegisterStartupScript(up, typeof(string), this.ClientID + "close_overlay_up", "close_progressing_modal();", true);
        }

        protected void close_overlay()
        {
            ScriptManager.RegisterStartupScript(this.Page, typeof(string), this.ClientID + "close_overlay", "close_progressing_modal();", true);
        }
        #endregion

        #region Message Box

        protected void give_error_message()
        {
            string function = "";
            function += DigitalAmele.WebApp.Tools.MessageBox.MessageBoxTemplates.get_general_error(true);

            ScriptManager.RegisterStartupScript(this.Page, typeof(string), this.ClientID + DateTime.Now.Millisecond, function, true);
        }
        protected void give_error_message(string msg)
        {
            string function = "";
            function = DigitalAmele.WebApp.Tools.MessageBox.MessageBoxTemplates.get_error_message(true, msg);
            ScriptManager.RegisterStartupScript(this.Page, typeof(string), this.ClientID + DateTime.Now.Millisecond, function, true);
        }
        protected void give_info_message(string msg)
        {
            string function = "";
            function = DigitalAmele.WebApp.Tools.MessageBox.MessageBoxTemplates.get_info_message(true, msg);
            ScriptManager.RegisterStartupScript(this.Page, typeof(string), this.ClientID + DateTime.Now.Millisecond, function, true);
        }
        protected void give_alert_message(string msg)
        {
            string function = "";
            function = DigitalAmele.WebApp.Tools.MessageBox.MessageBoxTemplates.get_alert_message(true, msg);
            ScriptManager.RegisterStartupScript(this.Page, typeof(string), this.ClientID + DateTime.Now.Millisecond, function, true);
        }
        protected void give_success_message(string message, bool redirect, string location)
        {
            string function = "";
            function += DigitalAmele.WebApp.Tools.MessageBox.MessageBoxTemplates.get_success_message(message, redirect, location);


            ScriptManager.RegisterStartupScript(this.Page, typeof(string), this.ClientID + DateTime.Now.Millisecond, function, true);
        }

        protected void give_error_message(UpdatePanel up)
        {
            string function = "";
            function += DigitalAmele.WebApp.Tools.MessageBox.MessageBoxTemplates.get_general_error(true);

            ScriptManager.RegisterStartupScript(up, typeof(string), this.ClientID + DateTime.Now.Millisecond, function, true);
        }
        protected void give_error_message(UpdatePanel up, string msg)
        {
            string function = "";
            function = DigitalAmele.WebApp.Tools.MessageBox.MessageBoxTemplates.get_error_message(true, msg);
            ScriptManager.RegisterStartupScript(up, typeof(string), this.ClientID + DateTime.Now.Millisecond, function, true);
        }
        protected void give_info_message(UpdatePanel up, string msg)
        {
            string function = "";
            function = DigitalAmele.WebApp.Tools.MessageBox.MessageBoxTemplates.get_info_message(true, msg);
            ScriptManager.RegisterStartupScript(up, typeof(string), this.ClientID + DateTime.Now.Millisecond, function, true);
        }
        protected void give_alert_message(UpdatePanel up, string msg)
        {
            string function = "";
            function = DigitalAmele.WebApp.Tools.MessageBox.MessageBoxTemplates.get_alert_message(true, msg);
            ScriptManager.RegisterStartupScript(up, typeof(string), this.ClientID + DateTime.Now.Millisecond, function, true);
        }
        protected void give_success_message(UpdatePanel up, string message, bool redirect, string location)
        {
            string function = "";
            function += DigitalAmele.WebApp.Tools.MessageBox.MessageBoxTemplates.get_success_message(message, redirect, location);

            ScriptManager.RegisterStartupScript(up, typeof(string), this.ClientID + DateTime.Now.Millisecond, function, true);
        }
        #endregion

        protected override void OnLoad(EventArgs e)
        {
            if (!IsPostBack)
            {
                if (SessionKeeper.get_value(SessionNames.Message) != null)
                {
                    MessageShow(SessionKeeper.get_value(SessionNames.Message).ToString(), (EnumMessageTypes)SessionKeeper.get_value(SessionNames.EnumMessageTypes));
                    SessionKeeper.remove(SessionNames.Message);
                    SessionKeeper.remove(SessionNames.EnumMessageTypes);
                }

                if (SessionKeeper.get_value(SessionNames.MemberUser) != null)
                {
                    memberUser = (MemberUser)SessionKeeper.get_value(SessionNames.MemberUser);
                }

                if (!AuthorityControl())
                {
                    SessionKeeper.add_is_not_contain(SessionNames.Message, "Girmek İstediğiniz Sayfaya Yetkiniz Yok.");
                    SessionKeeper.add_is_not_contain(SessionNames.EnumMessageTypes, EnumMessageTypes.Uyari);
                    Response.Redirect("~/Default.aspx");
                }

            }

            base.OnLoad(e);
        }


        public Boolean AuthorityControl()
        {
            String[] path = Request.Path.ToString().Split('/');
            List<String> folderNames = new List<string>();

            for (int i = 1; i < path.Count() - 1; i++)
            {
                folderNames.Add(path[i]);
            }
            String pagePath = path[path.Count() - 1];
            String pageName = pagePath.Substring(0, pagePath.Count() - 5);

            if (folderNames.Count != 0)
            {
                if (memberUser.FolderRoles != null)
                    foreach (Roles item in memberUser.FolderRoles)
                    {
                        foreach (var item1 in folderNames)
                        {
                            if (item.Name == item1)
                            {
                                return true;
                            }
                        }

                    }
            }
            else
                return true;
            if (memberUser.PageRoles != null)
                foreach (Roles item in memberUser.PageRoles)
                {

                    if (item.Name == pageName)
                    {
                        return true;
                    }

                }


            return false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="message"></param>
        /// <param name="messageType"></param>
        /// <param name="showTime"> mili saniye olacak</param>
        public void MessageShow(String message, EnumMessageTypes messageType, int showTime = 4000)
        {
            Control form = this.Master.FindControl("masterform");

            Label css = (Label)this.Master.FindControl("lblCss");

            string mesajResim = "";

            switch (messageType)
            {
                case EnumMessageTypes.Hata:
                    mesajResim = "hata.jpg";
                    break;
                case EnumMessageTypes.Bilgi:
                    mesajResim = "bilgi.jpg";
                    break;
                case EnumMessageTypes.Uyari:
                    mesajResim = "uyari.png";
                    break;
                case EnumMessageTypes.Onay:
                    mesajResim = "onay.png";
                    break;

                default:
                    break;
            }

            string cssS = @"  <style type=""text/css"">
                                        div.growlUI
                                        {
                    
                                            background-image: url(/Styles/" + mesajResim + @");
                                            background-repeat: no-repeat;
                                            background-attachment: 10px;
                                            background-position: 10px;
                                            -webkit-border-radius: 10px;
                                            -moz-border-radius: 10px;
                                             border-radius: 10px;
                                            behavior: url(/Styles/border-radius.htc);
                                           
                                        }
                                        div.growlUI h1, div.growlUI h2
                                        {
                                            color: white;
                                            padding: 5px 5px 5px 75px;
                                            text-align: left;
                                        }
                                    </style>";
            cssS += String.Format(@" <div class=""growlUI"" style=""display:none"">
                                                 <h1> {0} </h1>                                      
                                             </div>", message);
            css.Text = cssS;
            string function = @"$(document).ready(function () {             
                                                $.blockUI({
                                                    message: $('div.growlUI'),
                                                fadeIn: 700,
                                                fadeOut: 700,
                                                timeout:"+showTime+@",
                                                showOverlay: false,
                                                centerY: false,
                                                css: {
                                                width: '350px',
                                                top: '10px',
                                                left: '',
                                                right: '10px',
                                                border: 'none',
                                                padding: '5px',
                                                backgroundColor: '#000',
                                                opacity: .6,
                                                color: '#fff'
                                                }
                                                });
                             
                                             });";

            ScriptManager.RegisterStartupScript(form, typeof(string), this.ClientID + DateTime.Now.Millisecond, function, true);

        }

    }
}